using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_Controls_AdminSettings : IWS.Lib.Utilities.Web.FormControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			this.Initialize();
		}
		this.grdList.ItemCommand += new DataGridCommandEventHandler(grdList_ItemCommand);
	}

	void grdList_ItemCommand(object source, DataGridCommandEventArgs e)
	{
		Label lblKey = e.Item.FindControl("lblKey") as Label;
		if (lblKey != null)
		{
			IWS.Lib.SingleSignOn.Setting setting = new IWS.Lib.SingleSignOn.Setting(lblKey.Text);
			setting.Remove();

			this.MessageBox1.MessageType = IWS.Lib.Utilities.Web.MessageBox.Type.Normal;
			this.MessageBox1.ShowMessage("Successfully deleted setting [" + lblKey.Text + "]");

			this.RefreshData();
		}
	}

	protected override void InitializeCustom()
	{
		this.pnlNew.Visible = false;
		this.RefreshData();
	}
	protected override void OnBackClick(object sender, EventArgs e)
	{
		if (this.pnlNew.Visible)
		{
			this.pnlNew.Visible = false;
			this.txtKey.Text = "";
			this.txtValue.Text = "";
		}
		else
		{
			base.OnBackClick(sender, e);
		}
	}

	protected override void OnSaveClick(object sender, EventArgs e)
	{
		try
		{
			if (this.pnlNew.Visible)
			{
				//make sure it doesn't already exist.
				if (!IWS.Lib.SingleSignOn.Setting.Exists(txtKey.Text))
				{
					IWS.Lib.SingleSignOn.Setting setting = new IWS.Lib.SingleSignOn.Setting();
					setting.Key = txtKey.Text;
					setting.Value = txtValue.Text;
					setting.Save();
					ResetNewForm();
					this.pnlNew.Visible = false;
					this.RefreshData();
				}
				else
				{
					this.MessageBox1.MessageType = IWS.Lib.Utilities.Web.MessageBox.Type.Error;
					this.MessageBox1.ShowMessage("The new setting [" + txtKey.Text + "] already exists.");
				}
			}
			else
			{
				//perform save
				foreach (DataGridItem item in this.grdList.Items)
				{
					Label lblKey = item.FindControl("lblKey") as Label;
					TextBox txtValue = item.FindControl("txtValue") as TextBox;
					if (lblKey != null && txtValue != null)
					{
						IWS.Lib.SingleSignOn.Setting setting = new IWS.Lib.SingleSignOn.Setting(lblKey.Text);
						setting.Value = txtValue.Text;
						setting.Save();
					}
				}

				this.MessageBox1.MessageType = IWS.Lib.Utilities.Web.MessageBox.Type.Normal;
				this.MessageBox1.ShowMessage("Saved all settings successfully.");


				base.OnSaveClick(sender, e);
			}
		}
		catch (Exception ex)
		{
			IWS.Lib.SingleSignOn.Utility.LogException(System.Reflection.Assembly.GetExecutingAssembly(), ex);
			MessageBox1.ShowMessage(ex.Message);
		}
	}

	protected void cmdNew_Click(object sender, EventArgs e)
	{
		this.pnlNew.Visible = true;
	}
	private void ResetNewForm()
	{
		this.txtValue.Text = "";
		this.txtKey.Text = "";
	}
	private void RefreshData()
	{
		this.grdList.DataSource = IWS.Lib.SingleSignOn.Setting.ListAll();
		this.grdList.DataBind();
	}
}
